package beans;

import com.icesoft.faces.component.ext.HtmlSelectOneMenu;
import java.util.List;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseId;
import javax.faces.event.ValueChangeEvent;
import score.EnterScoreManagedBean;
import score.Status;

/**
 *
 * @author HipTeen
 */
public class GroupScoreCell {
    
    private double score;
    
    private Status status;
    
    private boolean scoreChanged;
    
    private int exercise;
    
    private List<Integer> rows;

    public GroupScoreCell(int exercise, List<Integer> rows) {
        this.status = Status.NOT_INSERTED;
        this.exercise = exercise;
        this.rows = rows;
    }
    
    public void scoreValueChange(ValueChangeEvent v) {
        if (v.getPhaseId().equals(PhaseId.ANY_PHASE)) {
            v.setPhaseId(PhaseId.INVOKE_APPLICATION);
            v.queue();
        } else {
           this.status = Status.EDITED;
           this.scoreChanged = true;
           this.score = (Double) v.getNewValue();
           for (UIComponent u: ((UIComponent) v.getSource()).getParent().getChildren()) {
                if (u instanceof HtmlSelectOneMenu) {
                    System.out.
                            println("GEFUNDEN!");
                    ((HtmlSelectOneMenu) u).setValue(Status.EDITED);
                }
            }
           FacesContext c = FacesContext.getCurrentInstance();
           EnterScoreManagedBean e = c.getApplication().evaluateExpressionGet(c, "#{enterScoreBean}", EnterScoreManagedBean.class);
           e.enterGroupsScore();
        }
           
    }
    
    public void statusValueChange(ValueChangeEvent v) {
        this.scoreChanged = true;
        this.status = (Status) v.getNewValue();
    }

    public int getExercise() {
        return exercise;
    }

    public void setExercise(int exercise) {
        this.exercise = exercise;
    }

    public List<Integer> getRows() {
        return rows;
    }

    public void setRows(List<Integer> rows) {
        this.rows = rows;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        
    }

    public boolean isScoreChanged() {
        return scoreChanged;
    }
    
    
}
